package com.lkm.app_ui.jz.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.jz.util.JzUtil;
import com.lkm.app_ui.jz.util.ZXingUtils;
import com.lkm.market2_0.entity.BasisEntity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 2024年3月22日 10:10:08
 * 把按键27改成微会员支付，按键26改成静态聚合支付。
 */
public class ShopScreenFragment extends MBaseFragment {

    public static ShopScreenFragment shopScreenFragment;

    public static ShopScreenFragment getInstance() {
        ShopScreenFragment f    = shopScreenFragment = new ShopScreenFragment();
        Bundle             data = new Bundle();
        f.setArguments(data);
        return f;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_shop_screen;
    }


    private TextView mMarketNameTv, mStallNameTv, mShopNameTv, mTraceTitleTv;
    private ImageView mHeadIv, mTraceIv;
    private Banner mBanner;


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        mMarketNameTv = findViewById(R.id.mMarketNameTv);
        mStallNameTv = findViewById(R.id.mStallNameTv);
        mShopNameTv = findViewById(R.id.mShopNameTv);
        mTraceTitleTv = findViewById(R.id.mTraceTitleTv);

        mHeadIv = findViewById(R.id.mHeadIv);
        mTraceIv = findViewById(R.id.mTraceIv);
        mBanner = findViewById(R.id.mBanner);

        initDataX();
    }

    private List<String> mBannerSrcList = new ArrayList<>();

    @SuppressLint("SetTextI18n")
    private void initDataX() {
        mBannerSrcList.clear();
        BasisEntity basisEntity = RunTimeData.steelyardInfo.basisEntity;
        if (basisEntity == null) basisEntity = new BasisEntity();
        String   mLicenseUrl  = basisEntity.mLicenseUrl;
        String   mCertificate = basisEntity.mCertificate;
        String[] mAdList      = basisEntity.mAdList;
        if (!TextUtils.isEmpty(mLicenseUrl)) mBannerSrcList.add(mLicenseUrl);
        if (!TextUtils.isEmpty(mCertificate)) mBannerSrcList.add(mCertificate);
        if (mAdList != null && mAdList.length > 0) {
            mBannerSrcList.addAll(Arrays.asList(mAdList));
        }

        if (mBannerSrcList.isEmpty()) {
            //TODO:添加一个默认图
            mBannerSrcList.add("https://jz-advert.oss-cn-shenzhen.aliyuncs.com/video/20240330/20240330154407_tnv8.png");
        }

        mBanner.setBannerStyle(BannerConfig.NOT_INDICATOR)
                .setImages(mBannerSrcList)
                .setBannerAnimation(Transformer.ZoomOut)
                .setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        if (path != null) {
                            JzUtil.setFitCenter(getContext(), (String) path, imageView);

                        }
                    }
                })
                .start();


        //头像
        String mHeadUrl = RunTimeData.steelyardInfo.basisEntity.mHeadUrl;
        if (TextUtils.isEmpty(mHeadUrl)) {
            mHeadIv.setImageResource(R.mipmap.ic_head_default);
        } else {
            JzUtil.setCircleImg(getContext(), mHeadUrl, mHeadIv);
        }
        //市场
        mMarketNameTv.setText(RunTimeData.steelyardInfo.orginfo.getName());
        //档口
        mStallNameTv.setText("档口号：" + RunTimeData.steelyardInfo.stallEntity.getStallNo());
        //商户名
        mShopNameTv.setText("姓名：" + RunTimeData.steelyardInfo.stallEntity.getName());
        //一户一码
        String mTraceQrUrl = RunTimeData.steelyardInfo.basisEntity.mTraceQrUrl;
        if (TextUtils.isEmpty(mTraceQrUrl)) {
            mTraceIv.setImageResource(R.mipmap.ic_pay_default);
        } else {
            mTraceIv.setImageBitmap(ZXingUtils.createQRCodeBitmap(mTraceQrUrl, 400,
                    "UTF-8", "H", "0", Color.BLACK,
                    Color.WHITE, null, null, 0.2f));
        }
        String title = RunTimeData.steelyardInfo.basisEntity.mTraceQrTitle;
        if (TextUtils.isEmpty(title)) mTraceTitleTv.setText("一户一码");
        else mTraceTitleTv.setText(title);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    protected boolean configIsLockKey() {
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void dismiss() {

        RouterUINavigation.getInstance().dismiss(this);
    }
}
